package com.scalar.chiptrack.sales.reports.actions;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.utils.DateUtil;
import com.scalar.chiptrack.utils.ExcelUtils;

import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.operations.reports.DataDumpReportsHelper;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.sales.reports.forms.RevenueShipmentReportForm;
import com.scalar.chiptrack.sales.dao.CustomerDAO;
import com.scalar.chiptrack.sales.dao.NewCustomerDAO;
import com.scalar.chiptrack.sales.dao.SalesOrderDAO;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.users.User;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import org.apache.poi.ss.usermodel.Workbook;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Srikanth.C
 * Date: Apr 14, 2007
 * Time: 11:21:51 AM
 * To change this template use File | Settings | File Templates.
 */
public class VendorShipmentReportAction extends ChipTrackAction
{
    private static final String ACTION_NAME = "/vendorShipmentReport.do";
    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response )
            throws Exception
    {

        String action = request.getParameter( "action" );
        HttpSession session = request.getSession();

        User user = ( User ) session.getAttribute( SessionKeys.USER_KEY );

        if( user == null )
        {
            session.setAttribute( SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString() );
            return ( mapping.findForward( "login" ) );
        }

        RevenueShipmentReportForm revenueShipmentReportForm = (RevenueShipmentReportForm) form;

        ArrayList vendorList = null;
        ArrayList productList = null;
        ArrayList customerList = null;
        ArrayList endCustomerList = null;
        ArrayList vendorShipmentList = null;
        Workbook workbook = null;
        String fileName = "";

        if ( action == null )
        {
            action = "";
        }
        try
        {
            if ( "vendorShipmentReport".equalsIgnoreCase(action) || "".equals(action) )
            {
                try
                {
                    //revenueShipmentReportForm.setCustomer("0");
                    if ( user.getUserType() == Constants.USER_TYPE_CUSTOMER )
                    {
                        revenueShipmentReportForm.setCustomer(user.getCustomerId()+"");
                    }
                    vendorList = VendorDAO.getAllVendorNamesByUserType( null, user );
                    customerList = CustomerDAO.getAllCustomerNamesByUserType(null,user);
                    endCustomerList = NewCustomerDAO.loadAllEndCustomersWithSOEndcustomersByUserType(null,-1,false,user);
                    productList = NewProductDAO.loadPartNumbers( null, 0, Constants.LOCATION_TYPE_FG, true, true, false, 0 );

                }
                catch( Exception e )
                {
                    vendorList         = new ArrayList();
                    productList        = new ArrayList();
                    customerList       = new ArrayList();
                    endCustomerList    = new ArrayList();

                    e.printStackTrace();
                }

                request.setAttribute( "vendorList", vendorList );
                request.setAttribute( "productList", productList );
                request.setAttribute( "customerList", customerList );
                request.setAttribute( "endCustomerList", endCustomerList );
            }
            else if ( "salesOrderInfoReport".equalsIgnoreCase(action)  )
            {
                ArrayList endUserList = null;
                ArrayList salesPersonList = null;

                try
                {
                    //revenueShipmentReportForm.setCustomer("0");
                    if ( user.getUserType() == Constants.USER_TYPE_CUSTOMER )
                    {
                        revenueShipmentReportForm.setCustomer(user.getCustomerId()+"");
                    }

                    customerList = CustomerDAO.getAllCustomerNamesByUserType(null,user);
                    endCustomerList = NewCustomerDAO.loadAllEndCustomersWithSOEndcustomersByUserType(null,-1,false,user);
                    salesPersonList = SalesOrderDAO.getAllSalesPersonsByUserType (null,user);
                    endUserList = NewCustomerDAO.loadAllEndUsersByUserType(null,-1, false,user);
                    productList = NewProductDAO.loadPartNumbers( null, 0, Constants.LOCATION_TYPE_FG, true, true, false, 0 );

                }
                catch( Exception e )
                {
                    productList         = new ArrayList();
                    customerList        = new ArrayList();
                    endCustomerList     = new ArrayList();
                    endUserList         = new ArrayList();
                    salesPersonList     = new ArrayList();

                    e.printStackTrace();
                }

                request.setAttribute( "salesPersonList", salesPersonList );
                request.setAttribute( "productList", productList );
                request.setAttribute( "customerList", customerList );
                request.setAttribute( "endCustomerList", endCustomerList );
                request.setAttribute( "endUserList", endUserList );
            }
            else if ( "shipmentReportbyLotNumber".equalsIgnoreCase(action) )
            {
                try
                {
                    productList = NewProductDAO.loadPartNumbers( null, 0, Constants.LOCATION_TYPE_FG, true, true, false, 0 );
                    customerList = CustomerDAO.getAllCustomerNamesByUserType(null,user);
                }
                catch( Exception e )
                {
                    productList        = new ArrayList();
                    customerList       = new ArrayList();

                    e.printStackTrace();
                }

                request.setAttribute( "productList", productList );
                request.setAttribute( "customerList", customerList );
            }
            else if ( "boardAndKitShipmentReport".equalsIgnoreCase(action) )
            {
                try
                {
                    productList = NewProductDAO.loadPartNumbers( null, 3, Constants.LOCATION_TYPE_FINISHED_GOODS, true, true, false, 0 );
                    customerList = CustomerDAO.getAllCustomerNamesByUserType(null,user);
                }
                catch( Exception e )
                {
                    productList        = new ArrayList();
                    customerList       = new ArrayList();

                    e.printStackTrace();
                }

                request.setAttribute( "productList", productList );
                request.setAttribute( "customerList", customerList );
            }
            else if ( "generateVendorShipmentXls".equalsIgnoreCase(action) )
            {
                int orderTypeInt = -1;

                String customerIds  = request.getParameter( "prompt0" );
                String vendorId     = request.getParameter( "prompt1" );
                String endCustomer  = request.getParameter( "prompt2" );
                String soNubmer     = request.getParameter( "prompt3" );
                String productId    = request.getParameter( "prompt4" );
                String category     = request.getParameter( "prompt5" );
                String fromDate     = request.getParameter( "prompt6" );
                String toDate       = request.getParameter( "prompt7" );
                String reportType   = request.getParameter( "prompt8" );
                String defaultPrompt = request.getParameter( "prompt9" );

                orderTypeInt = Integer.parseInt(category);

                vendorShipmentList = NewCustomerDAO.loadVendorShipmentReprot( null,customerIds,vendorId,endCustomer,soNubmer,productId,orderTypeInt,fromDate,toDate,reportType,defaultPrompt);

                workbook =  DataDumpReportsHelper.getWorkbook( vendorShipmentList, "Vendor Shipment Report",fromDate,toDate,null );
                fileName = "VendorShipmentReport_"+ DateUtil.getTimeStamp();
                
                ExcelUtils.generateExcelReport( workbook, fileName, response);
            }
        }
        catch ( Exception e)
        {
            //errors.put ( "Could not generate excel file", null );
            e.printStackTrace();
        }

        if( "vendorShipmentReport".equalsIgnoreCase(action) )
        {
            return ( mapping.findForward( "vendorShipmentReport" ) );
        }
        else if( "shipmentReportbyLotNumber".equalsIgnoreCase(action) )
        {
            return ( mapping.findForward( "shipmentReportbyLotNumber" ) );
        }
        else if( "boardAndKitShipmentReport".equalsIgnoreCase(action) )
        {
            return ( mapping.findForward( "boardAndKitShipmentReport" ) );
        }
        else if( "salesOrderInfoReport".equalsIgnoreCase(action) )
        {
            return ( mapping.findForward( "salesOrderInfoReport" ) );
        }

        return null;
    }

}
